Python random.shuffle()函数 您所在的位置:网站首页 python shuffle函数 Python random.shuffle()函数

Python random.shuffle()函数

2023-11-27 17:14| 来源: 网络整理| 查看: 265

Python 的随机包有一个内置的函数 shuffle()。序列可以在 Python 中使用它(如列表或元组)进行洗牌;随机排列意味着更改集合元素的索引。

random.shuffle() 的语法我们使用 shuffle() 函数随机更改任何可迭代对象的元素索引。此函数不返回新列表。它会更改原始列表。

shuffle() 函数的语法

random.shuffle(sequence, function)

函数参数:

sequence:这可以接受任何 Python 可迭代对象。function:此参数是可选的。此函数的默认值是 random() 函数。如果传递一个用户定义的函数,那么该函数必须返回一个范围 (0, 1) 的值。

返回:此函数不返回任何内容。

shuffle() 函数示例随机排列列表

# Python program to shuffle a list using random.shuffle method # importing the random module import random # Creating a list list_ = [1, 2, 3, 4, 5, 6] print("The original list is as follows: ") print(list_) # Shuffling the list first time random.shuffle(list_) print("After shuffling the list the first time: ") print(list_) # Shuffling the list a second time random.shuffle(list_) print("After shuffling the list a second time: ") print(list_)

运行结果:

The original list is as follows: [1, 2, 3, 4, 5, 6] After shuffling the list the first time: [4, 3, 1, 6, 5, 2] After shuffling the list a second time: [5, 6, 4, 2, 1, 3]

使用用户定义的函数随机排列列表

# Python program to shuffle the list as per a function # importing the random module import random # user-defined function to shuffle the list def shuffling_function(): return 0.5 list_ = [1, 2, 3, 4, 5, 6] print("The original list is as follows: ") print(list_) # The shuffling_function will return the same value each time. # hence the order of the shuffle will remain the same even after shuffling multiple times. random.shuffle(list_, shuffling_function) print("After shuffling the list the first time: ") print(list_) list_ = [1, 2, 3, 4, 5, 6] random.shuffle(list_, shuffling_function) print("After shuffling the list a second time: ") print(list_)

运行结果:

The original list is as follows: [1, 2, 3, 4, 5, 6] After shuffling the list the first time: [1, 6, 2, 5, 3, 4] After shuffling the list a second time: [1, 6, 2, 5, 3, 4]

随机洗牌不到位众所周知,Python shuffle() 函数就地对给定的集合进行操作,并且不返回任何内容或返回 None,这意味着它会随机重新排列原始列表的元素并在其内存位置对其进行修改。但是通常需要原始列表或集合。

现在描述的方法允许保留原始列表并获得随机列表。此替代方法返回一个新的、打乱列表,而不是更改原始列表。

在更改订单之前,我们将复制原始列表以保留它。

# Python program to shuffle a list using random.sample but not in place # Importing the random module import random # Creating a list original_list = [1, 2, 3, 4, 5, 6] # Creating a copy of the original list copy_list = original_list.copy() # shuffling the copy of the list random.shuffle(copy_list) print("The original list is as follows: ") print(original_list) print("After shuffling the list: ") print(copy_list)

运行结果:

The original list is as follows: [1, 2, 3, 4, 5, 6] After shuffling the list: [5, 2, 6, 4, 3, 1]

以相同的顺序同时随机排列两个列表假设有两个 Python 列表。我们需要洗牌这两个列表以保持洗牌顺序。例如,一个列表包含字母,另一个列表包含相应的数字。下面来看看如何洗牌两个列表并保持它们的顺序。

# Pyhton program to shuffle two lists in the same order # Importing the random module import random # Creating the first list list1 = ['a', 'b', 'c', 'd', 'e'] # Creating a second list list2 = [1, 2, 3, 4, 5] # Both of the lists before shuffling them print("The first list: ", list1) print("The second list: ", list2) # We will use the zip function to map the indices of the lists to maintain the same order map_index = list(zip(list1, list2)) # Shuffling the indices random.shuffle(map_index) # making a separate list list1_s, list2_s = zip(*map_index) # Both the lists after shuffling them print("The first list after shuffling: ", list1_s) print("The second list after shuffling: ", list2_s) # Elements at index 2 of both the lists print(list1_s[2], list2_s[2])

运行结果:

The first list: ['a', 'b', 'c', 'd', 'e'] The second list: [1, 2, 3, 4, 5] The first list after shuffling: ('c', 'e', 'a', 'd', 'b') The second list after shuffling: (3, 5, 1, 4, 2)

洗牌 NumPy 多维数组Python 的 Numpy 模块有一个名为 numpy.random 的包。该包用于生成随机数据。在这个例子中将使用 Python 的 Numpy 模块创建一个 2D 数组。然后使用 numpy.random.shuffle() 函数来洗牌这个多维数组。

# Python program to shuffle a multidimensional array # Importing the numpy module import numpy as np # Creating a 2D array using numpy print('The original 2D array') array = np.arange(1, 24, 2) array = array.reshape(6, 2) print(array) print('The shuffled 2D array') np.random.shuffle(array) print(array)

运行结果:

The original 2D array [[ 1 3] [ 5 7] [ 9 11] [13 15] [17 19] [21 23]] The shuffled 2D array [[13 15] [ 1 3] [21 23] [17 19] [ 9 11] [ 5 7]]

随机排列项目并每次获得相同的结果在本节中将看到如何使用随机包的种子方法来洗牌列表,以便每次调用 shuffle 函数时,它都会以相同的顺序洗牌原始列表。因此,每次洗牌后都会得到相同的列表。

PRNG如何工作?

Python 的随机库并不是完全随机的。它使用一种称为伪随机数生成器 (PRNG) 的方法。这意味着它是一个确定性系统。随机模块的种子值充当随机数生成的基础。默认情况下,随机包将当前系统时间作为其种子值。可以更改此种子值,因此可以更改 random.shuffle() 函数的输出。下面看看如何使用这个值。

# Python program to show how to use a seed value to produce the same result after shuffling # Importing the random module import random # Creating a list of numbers numbers = [1, 3, 5, 7, 9, 11, 13, 15] print("The original list is: ", numbers) # shuffling the original list 4 times for i in range(4): # Specifying the seed value before every shuffling instance random.seed(4) # Shuffling the list with pre-defined seed value random.shuffle(numbers) print("The shuffled list is: ", numbers)

运行结果:

The original list is: [1, 3, 5, 7, 9, 11, 13, 15] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7] The shuffled list is: [3, 13, 11, 9, 15, 1, 5, 7]

上一篇: Python3异常处理 下一篇:哥,这回真没有了


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有